package com.huawei.cloud.phone.management.infrastructure;

import com.huawei.cloud.phone.management.common.BeanFactory;
import com.huawei.cloud.phone.management.common.GlobalConfig;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.cph.v1.CphClient;
import com.obs.services.ObsClient;

import java.util.LinkedList;
import java.util.List;

public final class ClientFactory {
    private ClientFactory() {
    }

    /**
     * 获取CPH的SDK客户端
     *
     * @return CPH客户端
     */
    public static CphClient getCphClient() {
        GlobalConfig globalConfig = BeanFactory.getBean(GlobalConfig.class);
        BasicCredentials credential = new BasicCredentials();
        credential.setAk(globalConfig.getAk());
        credential.setSk(globalConfig.getSk());
        Region region = new Region();
        region.setId(globalConfig.getRegion());
        List<String> endpoints = new LinkedList<>();
        endpoints.add(String.format("cph.%s.myhuaweicloud.com", globalConfig.getRegion()));
        return CphClient.newBuilder().withCredential(credential).withRegion(region)
                .withEndpoints(endpoints)
                .build();
    }

    /**
     * 获取OBS的SDK客户端
     *
     * @return OBD客户端
     */
    public static ObsClient getObsClient() {
        GlobalConfig globalConfig = BeanFactory.getBean(GlobalConfig.class);
        String endPoint = String.format("https://obs.%s.myhuaweicloud.com", globalConfig.getRegion());
        return new ObsClient(globalConfig.getAk(), globalConfig.getSk(), endPoint);
    }
}
